package com.chinacoal.microservice.impl.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.chinacoal.microservice.api.model.QueryUtaskCatParam;
import com.chinacoal.microservice.api.model.entity.UtaskCatEntity;
import com.chinacoal.microservice.impl.mapper.UtaskCatMapper;
import com.chinacoal.microservice.impl.service.UtaskCatAttrMapService;
import com.chinacoal.microservice.impl.service.UtaskCatService;
import com.chinacoal.microservice.impl.util.UtaskCommonFun;
import com.chinacoal.microservice.util.PrimaryKey;

@Service
public class UtaskCatServiceImpl extends ServiceImpl<UtaskCatMapper, UtaskCatEntity> implements UtaskCatService {

    @Resource
    private UtaskCatMapper utaskCatMapper;
    @Resource
    private UtaskCatAttrMapService utaskCatAttrMapService;

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void addTaskReg(UtaskCatEntity utaskCatEntity) throws Exception {
		
		// 校验
		String sysId = utaskCatEntity.getSysId();
		String typeCode = utaskCatEntity.getTypeCode();
		Integer count = utaskCatMapper.getTaskCatCount(sysId, typeCode);
		if(count != null && count > 0) throw new Exception("该待办类型已存在，请勿重复添加");
		
		// 计算类型层级
		String catId = PrimaryKey.newKey();
		String upTypeCode = utaskCatEntity.getUpTypeCode();
		if(StringUtils.isEmpty(upTypeCode)) {
			String typeSeq = "." + catId + ".";
			utaskCatEntity.setUpTypeCode("-1");
			utaskCatEntity.setTypeSeq(typeSeq);
		}else {
			UtaskCatEntity upUtaskCatEntity = utaskCatMapper.selectById(upTypeCode);
			String typeSeq = upUtaskCatEntity.getTypeSeq() + catId + ".";
			utaskCatEntity.setTypeSeq(typeSeq);
		}
		
		// 执行新增 
		utaskCatEntity.setCatId(catId);
		utaskCatMapper.insert(utaskCatEntity);
	}

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void updateTaskReg(UtaskCatEntity utaskCatEntity) throws Exception {

		// 计算类型层级
		String catId = utaskCatEntity.getCatId();
		String upTypeCode = utaskCatEntity.getUpTypeCode();
		if(StringUtils.isEmpty(upTypeCode)) {
			String typeSeq = "." + catId + ".";
			utaskCatEntity.setUpTypeCode("-1");
			utaskCatEntity.setTypeSeq(typeSeq);
		}else {
			UtaskCatEntity upUtaskCatEntity = utaskCatMapper.selectById(upTypeCode);
			String typeSeq = upUtaskCatEntity.getTypeSeq() + catId + ".";
			utaskCatEntity.setTypeSeq(typeSeq);
		}
		
		// 执行修改
		utaskCatMapper.updateById(utaskCatEntity);
		
		// 校验
		String sysId = utaskCatEntity.getSysId();
		String typeCode = utaskCatEntity.getTypeCode();
		Integer count = utaskCatMapper.getTaskCatCount(sysId, typeCode);
		if(count != null && count > 1) throw new Exception("该待办类型已存在，请检查");
	}

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void deleteTaskReg(String catId) throws Exception {
		
		// 校验
		Integer count = utaskCatAttrMapService.getCatAttrMapCount(null, catId, null);
		if(count != null && count > 0) throw new Exception("该待办类型扩展属性已被使用，请勿删除");
	
		// 执行删除
		utaskCatMapper.deleteById(catId);
	}

	@Override
	public Page<UtaskCatEntity> getList(Integer page, Integer pageSize, QueryUtaskCatParam param) throws Exception{
		
    	Page<UtaskCatEntity> utaskCatPage = UtaskCommonFun.getPage(page, pageSize);
		
		List<UtaskCatEntity> utaskCatList = utaskCatMapper.findList(utaskCatPage, param);
		
		return utaskCatPage.setRecords(utaskCatList);
	}

	@Override
	public List<UtaskCatEntity> getTaskCatBySysId(String sysId) throws Exception{
		return utaskCatMapper.getTaskCatBySysId(sysId);
	}

	@Override
	public List<UtaskCatEntity> getLowerTaskCat(String upCatId) throws Exception {
		return utaskCatMapper.getLowerTaskCat(upCatId);
	}
}
